﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using WechatDAL;
using WechatModel;
using Model;


namespace BLL
{
    public class MessageEntBll
    {
        private static readonly W_MessageEntDal wdal = new W_MessageEntDal();

        /// <summary>
        /// 企业号发送文本消息
        /// </summary>
        /// <param name="touser"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public bool SendText(int agentid, string touser, string toparty, string totag, string data)
        {
            return SendMessage(agentid, touser, toparty, totag, WMessageType.Text, data);
        }

        /// <summary>
        /// 向员工发送消息
        /// </summary>
        /// <param name="agentid">应用ID</param>
        /// <param name="touser">用户（可多个，用“|”隔开）</param>
        /// <param name="toparty">部门（非必要，可多个，用“|”隔开）</param>
        /// <param name="totag">标签（非必要，可多个，用“|”隔开）</param>
        /// <param name="msgType">消息类型</param>
        /// <param name="data">消息内容</param>
        /// <returns></returns>
        private bool SendMessage(int agentid, string touser, string toparty, string totag, string msgType, string data)
        {
            string content = "{{\"touser\":\"{0}\",\"toparty\":\"{1}\",\"totag\":\"{2}\",\"msgtype\":\"{3}\",\"agentid\":\"{4}\",\"{5}\":{{{6}}}}}";
            string body = "";
            if (msgType == WMessageType.Text)
            {
                body = string.Format("\"content\":\"{0}\"", data);
            }
            else if (msgType == WMessageType.Image || msgType == WMessageType.Voice)
            {
                body = string.Format("\"media_id\":\"{0}\"", data);
            }
            else if (msgType == WMessageType.Video)
            {
                body = data;
            }
            else if (msgType == WMessageType.News)
            {
                body = string.Format("\"articles\":{0}", data);
            }
            content = string.Format(content, touser, toparty, totag, msgType, agentid, msgType, body);
            return wdal.SendMessage(touser, content);
        }
    }
}
